package com.ym.b2b.provider.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.commons.log.CusLog;
import com.ym.b2b.provider.service.EmCProviderService;
import com.ym.b2b.provider.sqlbean.EmCProvider;
import com.ym.b2b.user.service.EmCUserService;
import com.ym.b2b.user.sqlbean.EmCUser;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;
import com.zc.export.ExcelUtil;

/**
 * @ClassName: EmCProviderController
 * @Description: TODO
 * @Author tangxj
 * @Date 2015-1-15 上午11:21:47
 * @Modify
 * @CopyRight B2B小组
 */
@Controller
@RequestMapping("/emcprovider")
public class ManagerEmCProviderController extends BaseController {

    @Autowired
    private EmCProviderService emCProviderService;
    @Autowired
    private EmCUserService     emCUserService;

    @RequestMapping("searchPageProvider.do")
    @ResponseBody
    public Result searchPageProvider(EmCProvider emCProvider, Page<EmCProvider> page, HttpServletRequest req) {

        Result result = new Result();
        try {
            result = emCProviderService.searchPageEmCProvider(page, emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;

    }

    @RequestMapping(value = "searchPageProviderExcel.do")
    @ResponseBody
    public Result searchPageProviderExcel(EmCProvider emCProvider, Page<EmCProvider> page, String loadType) {
        Result ret = new Result();
        try {
            if (loadType == null || loadType.equals("")) {
                ret.setCode(Result.CODE_100);
                ret.setResult("loadType 参数 '数量'  不能为空!");
                return ret;
            }
            page.setPageable("true");
            page.setPageSize(Integer.parseInt(loadType));
            page.setStartRow(1);
            ret = emCProviderService.searchPageEmCProvider(page, emCProvider);
            ExcelUtil<EmCProvider> excelUtil = new ExcelUtil<EmCProvider>();
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("model", ret.getRows());
            ret = excelUtil.createExcel("providerListExcel", map);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            ret.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return ret;
    }

    /***
     * @Title: toAddProvider
     * @Description: 跳转至添加页面
     * @Param @param req null
     * @Param @return
     * @Return ModelAndView
     * @Throws
     */
    @RequestMapping("toAddProvider.do")
    public ModelAndView toAddProvider(EmCProvider emCProvider, HttpServletRequest req) {
        return new ModelAndView("redirect:/views/jsp/customer/provider/provider_detail.jsp?code="
                                + emCProvider.getProviderCode());
    }

    @RequestMapping("showEmCProviderDetail.do")
    @ResponseBody
    public Result showEmCProviderDetail(EmCProvider emCProvider, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCProviderService.showEmCProviderDetail(emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "insertProvider.do")
    @ResponseBody
    public Result insertProvider(EmCProvider emCProvider, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 新增供应商", "USER", 2);
        Result result = new Result();
        try {
            result = emCProviderService.insertProvider(emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    @RequestMapping("updateProvider.do")
    @ResponseBody
    public Result updateProvider(EmCProvider emCProvider, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 修改供应商", "USER", 2);
        Result result = new Result();
        try {
            result = emCProviderService.updateProvider(emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除一条记录
    @RequestMapping("deleteProvider.do")
    @ResponseBody
    public Result deleteProvider(EmCProvider emCProvider, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCProviderService.deleteEmCProvider(emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 删除多条记录内容
    @RequestMapping("deleteMultipleEmCProviders.do")
    @ResponseBody
    public Result deleteMultipleEmCProviders(String providerIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCProviderService.deleteMultipleEmCProviders(providerIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 批量审批
    @RequestMapping("editMultipleEmCProvidersStatus.do")
    @ResponseBody
    public Result editMultipleEmCProvidersStatus(String providerIdList, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCProviderService.editMultipleEmCProvidersStatus(providerIdList);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    // 批量修改查询数据的开始日期
    @RequestMapping("batchUpdateProvider.do")
    @ResponseBody
    public Result batchUpdateProvider(EmCProvider emCProvider, HttpServletRequest req) {
        Result result = new Result();
        try {
            result = emCProviderService.batchUpdateProvider(emCProvider);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
    
    @RequestMapping(value = "addProviderUser.do")
    @ResponseBody
    public Result addProviderUser(EmCUser emCUser, HttpServletRequest req) {
    	CusLog.writeLog(getManagerTokenUser(req).getUserName() + " 添加供应商下登录账号", "USER", 2);
        Result result = new Result();
        try {
            result = emCUserService.insertUser(emCUser);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

}
